//
//  HTWayBillVC.m
//  TruckUser
//
//  Created by Ting Han on 2017/10/18.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTWayBillVC.h"
#import "UIView+Layout.h"
#import "HTWayBillCell.h"
#import "HTRefreshMoreModel.h"

#import "HTWayBillOnVC.h"

@interface HTWayBillVC ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *tapLeft;
@property (weak, nonatomic) IBOutlet UIView *lineLeft;
@property (weak, nonatomic) IBOutlet UILabel *labelLeft;

@property (weak, nonatomic) IBOutlet UIView *tapRight;
@property (weak, nonatomic) IBOutlet UIView *lineRight;
@property (weak, nonatomic) IBOutlet UILabel *labelRight;

@property (nonatomic,weak) UIView *selectedView;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *safeBottomHeight;


@property (nonatomic,assign) NSInteger pageNumber;
@property (nonatomic,strong) HTRefreshMoreModel *refreshMoreModel;
@property (nonatomic,assign) NSInteger selectedIndex;
@end

@implementation HTWayBillVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupHeadView];
    [self setupTableView];
    self.pageNumber = 1;
    self.selectedIndex = 2;
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}
- (void)setupTableView{
    self.safeBottomHeight.constant = IPHONE_SAFEBOTTOMAREA_HEIGHT;
    self.tableView.width = kHTScreenWidth;
    self.tableView.backgroundColor = kBackgroundColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"HTWayBillCell" bundle:nil] forCellReuseIdentifier:WayBillCell];
    self.tableView.estimatedRowHeight =0;
    self.tableView.estimatedSectionHeaderHeight =0;
    self.tableView.estimatedSectionFooterHeight =0;
    [self configTableHeader];
    [self configTableFooter];
    self.tableView.mj_header = self.header;
    self.tableView.mj_footer = self.footer;
    self.noDataView = [[HTNoDataView alloc]initWithFrame:self.tableView.bounds];
}

- (void)tapGesture:(UITapGestureRecognizer *)sender{
    
    if(sender.view.tag == 141){//left
        if (self.selectedView == self.tapLeft) {
            return;
        }
        self.selectedView = self.tapLeft;
        [self selectedLeft];
        self.selectedIndex = 2;
        [self.tableView.mj_header beginRefreshing];
    }else{//right
        if (self.selectedView == self.tapRight) {
            return;
        }
        self.selectedView = self.tapRight;
        [self selectedRight];
        self.selectedIndex = 3;
        [self.tableView.mj_header beginRefreshing];
    }
}

#pragma mark --- tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.selectedView == self.tapLeft) {
         self.noDataView.showMessage = @"进行中的订单将在这里显示";
    }else{
         self.noDataView.showMessage = @"已完成的订单将在这里显示";
    }
    [tableView tableViewDisplayView:self.noDataView ifNecessaryForRowCount:self.dataSource.count];
    return self.dataSource.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 112;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HTWayBillCell *cell = (HTWayBillCell *)[tableView dequeueReusableCellWithIdentifier:WayBillCell forIndexPath:indexPath withController:self];
    cell.waybillModel = self.dataSource[indexPath.row];
    __weak typeof(UITableView) *WeakTableView = self.tableView;
    cell.refreshBlock = ^{
        [WeakTableView.mj_header beginRefreshing];
    };
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HTWayBillOnVC *on = [[HTWayBillOnVC alloc]init];
    on.wayBillModel = self.dataSource[indexPath.row];
    on.wayBillType = [[(HTWayBillModel *) self.dataSource[indexPath.row] state]integerValue];
    [self.navigationController pushViewController:on animated:YES];
}

#pragma mark --- headView
- (void)setupHeadView{
    self.title = @"运单";
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
    self.tapLeft.tag = 141;
    [self.tapLeft addGestureRecognizer:tap1];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
    self.tapRight.tag = 142;
    [self.tapRight addGestureRecognizer:tap2];
    self.selectedView = self.tapLeft;
    [self.lineLeft setHidden:NO];
    [self.lineRight setHidden:YES];
    self.labelLeft.textColor = kBlueColor;
    self.labelRight.textColor = [UIColor darkGrayColor];
    
}

- (void)selectedLeft{
    [self.lineLeft setHidden:NO];
    [self.lineRight setHidden:YES];
    self.labelLeft.textColor = kBlueColor;
    self.labelRight.textColor = [UIColor darkGrayColor];
}
- (void)selectedRight{
    [self.lineLeft setHidden:YES];
    [self.lineRight setHidden:NO];
    self.labelLeft.textColor = [UIColor darkGrayColor];
    self.labelRight.textColor = kBlueColor;
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

#pragma mark --- 网络请求
- (void)loadMoreData{
    [self.tableView.mj_header endRefreshing];
    if (self.dataSource.count == 0) {
        [self.tableView.mj_footer endRefreshing];
        self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        return;
    }
    self.pageNumber ++;
    if (self.refreshMoreModel && [self.refreshMoreModel.total integerValue] < self.pageNumber) {
        [self.tableView.mj_footer endRefreshing];
        self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        return;
    }
    [self netLoadWaybillWithPage:self.pageNumber];
}
- (void)loadNewData{
    [self.tableView.mj_footer endRefreshing];
    self.pageNumber = 1;
    [self netLoadWaybillWithPage:1];
}
- (void)netLoadWaybillWithPage:(NSInteger)page{
    [HTHttpRequestManager requestWaybillListWithState:[NSString stringWithFormat:@"%ld",self.selectedIndex] page:[NSString stringWithFormat:@"%ld",page] success:^(id  _Nullable responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        DBLog(@"运单列表：%@",responseObject);
        if (!responseObject) {
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            return;
        }
        if (self.pageNumber == 1) {
            //代表是刷新的
            [self.dataSource removeAllObjects];
        }
        NSArray *result = responseObject[@"data"][@"data"];
        NSMutableArray *dataResult = [HTWayBillModel mj_objectArrayWithKeyValuesArray:result];
        [self.dataSource addObjectsFromArray:dataResult];
        NSDictionary *dic = responseObject[@"data"][@"meta"];
        self.refreshMoreModel = [HTRefreshMoreModel mj_objectWithKeyValues:dic[@"pagination"]];
        NSString *currentPage = self.refreshMoreModel.current_page;
        self.pageNumber = [currentPage integerValue];
        [self.tableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    
}


@end
